package Stream;

import java.util.Optional;
import java.util.Random;
import java.util.stream.Stream;

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "姓名：" + name;
    }

    static Random rand = new Random(39);

    public static Student smorse() {
        switch (rand.nextInt(6)) { // 获取大于等于0小于4的随机数
            case 4:
                return new Student("曹长卿", 18);
            case 2:
                return new Student("李玉斧", 22);
            case 5:
                return new Student("吕洞宾", 25);
            case 0:
                return new Student("徐凤年",18);
            default:
                return null;
        }
    }

    public static Stream<Optional<Student>> stream() { // 自定义生成流的函数
        return Stream.generate(Student::smorse)
                .map(student -> Optional.ofNullable(student)); // 转换类型
    }


}
